package com.kedacom.demo.healthcheck;

import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.exception.MemcachedException;

/**
 * Memcache健康检查
 * @author luocanfeng
 * @date 2016年9月2日
 */
public class MemcacheHealthCheckItem extends HealthCheckItem {

	private static final String MEMCACHED_CHECK_KEY = "health_check_key_memcached";

	/** 待验证的memcachedClient */
	private MemcachedClient memcachedClient;

	{
		type = Type.Memcache;
	}

	public MemcacheHealthCheckItem() {
		type = Type.Memcache;
	}

	public MemcacheHealthCheckItem(String name) {
		this();
		this.name = name;
	}

	public MemcacheHealthCheckItem(String name, MemcachedClient memcachedClient) {
		this(name);
		this.memcachedClient = memcachedClient;
	}

	/**
	 * 检查Memcache是否正常
	 */
	@Override
	public boolean doCheck() {
		try {
			return memcachedClient.set(MEMCACHED_CHECK_KEY, 1000, true);
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (MemcachedException e) {
			e.printStackTrace();
		}
		return false;
	}

	public MemcachedClient getMemcachedClient() {
		return memcachedClient;
	}

	public void setMemcachedClient(MemcachedClient memcachedClient) {
		this.memcachedClient = memcachedClient;
	}

}
